package com.taropan.wallpaper.ui;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.taropan.wallpaper.R;
import com.taropan.wallpaper.model.HomepageInfo;
import com.taropan.wallpaper.model.KeywordEntry;
import com.taropan.wallpaper.model.KeywordInfo;
import com.taropan.wallpaper.model.LastUpdatedWallpaper;
import com.taropan.wallpaper.util.Consts;
import com.taropan.wallpaper.util.HttpUtils;
import com.taropan.wallpaper.util.UICore;
import com.taropan.wallpaper.util.Utils;
import com.taropan.wallpaper.widget.Tag;
import com.taropan.wallpaper.widget.TagCloudView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;

public class SearchActivity extends BaseActivity implements Callback{

	private static final String TAG = SearchActivity.class.getSimpleName();
	
	private static final int EXEU_GET_KEYWORD_INFO = 0;

	
	private TagCloudView tagCloudView = null;

	private Handler handler;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_search);
		Utils.dout(TAG, "onCreate");
		initHead();
		tv_head.setVisibility(View.INVISIBLE);
		LinearLayout layout_content = (LinearLayout) this.findViewById(R.id.layout_content);
		
		handler = new Handler(this);
		
		List<Tag> myTagList= createTags();
		tagCloudView = new TagCloudView(this, CanvasWidth, CanvasHeight - 64*2, myTagList);
		
		layout_content.addView(tagCloudView);
		
		UICore.eventTask(this, SearchActivity.this, EXEU_GET_KEYWORD_INFO, null, null);
		
	}
	@Override
	protected void onResume() {
		super.onResume();
		Utils.dout(TAG, "onResume");
	}

	@Override
	public void execute(int mes, Object obj) {
		super.execute(mes, obj);
		switch (mes) {
		case EXEU_GET_KEYWORD_INFO:
			getKeyWordlist();
			break;

		default:
			break;
		}
	}
	
	private void getKeyWordlist() {
		StringBuffer url = new StringBuffer(Consts.API_WALLPAPER_KEYWORD);
		Utils.dout("getKeyWordlist url: " + url.toString());
		try {
			
			String results = HttpUtils.doGet(url.toString());

			if (results == null) {
				return;
			}
			Utils.dout("getKeyWordlist results:" + results.toString());
			Gson gson = new Gson();
			KeywordInfo keywordInfo = gson.fromJson(results, KeywordInfo.class);
			List<KeywordEntry> wallpaperKeywords = keywordInfo.getWallpaperKeyword();
			Message localMessage = Message.obtain(handler, EXEU_GET_KEYWORD_INFO, wallpaperKeywords);
			handler.sendMessage(localMessage);	
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private List<Tag> createTags(){
		//create the list of tags with popularity values and related url
		List<Tag> tempList = new ArrayList<Tag>();

		tempList.add(new Tag("Google", 7, "http://www.google.com"));  //1,4,7,... assumed values for popularity
		tempList.add(new Tag("Yahoo", 3, "www.yahoo.com"));
		tempList.add(new Tag("CNN", 4, "www.cnn.com"));
		tempList.add(new Tag("MSNBC", 5, "www.msnbc.com"));
		tempList.add(new Tag("CNBC", 5, "www.CNBC.com"));
		tempList.add(new Tag("Facebook", 7, "www.facebook.com"));
		tempList.add(new Tag("Youtube", 3, "www.youtube.com"));
		tempList.add(new Tag("BlogSpot", 5, "www.blogspot.com"));
		tempList.add(new Tag("Bing", 3, "www.bing.com"));
		tempList.add(new Tag("Wikipedia", 8, "www.wikipedia.com"));
		tempList.add(new Tag("Twitter", 5, "www.twitter.com"));
		tempList.add(new Tag("Msn", 1, "www.msn.com"));
		tempList.add(new Tag("Amazon", 3, "www.amazon.com"));
		tempList.add(new Tag("Ebay", 7, "www.ebay.com"));
		tempList.add(new Tag("LinkedIn", 5, "www.linkedin.com"));
		tempList.add(new Tag("Live", 7, "www.live.com"));
		tempList.add(new Tag("Microsoft", 3, "www.microsoft.com"));
		tempList.add(new Tag("Flicker", 1, "www.flicker.com"));
		tempList.add(new Tag("Apple", 5, "www.apple.com"));
		tempList.add(new Tag("Paypal", 5, "www.paypal.com"));
		tempList.add(new Tag("Craigslist", 7, "www.craigslist.com"));
		tempList.add(new Tag("Imdb", 2, "www.imdb.com"));
		tempList.add(new Tag("Ask", 4, "www.ask.com"));
		tempList.add(new Tag("Weibo", 1, "www.weibo.com"));
		tempList.add(new Tag("Tagin!", 8, "http://scyp.idrc.ocad.ca/projects/tagin"));
		tempList.add(new Tag("Shiftehfar", 8, "www.shiftehfar.org"));
		tempList.add(new Tag("Soso", 5, "www.google.com"));
		tempList.add(new Tag("XVideos", 3, "www.xvideos.com"));
		tempList.add(new Tag("BBC", 5, "www.bbc.co.uk"));
		return tempList;
	}
	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case EXEU_GET_KEYWORD_INFO:
			List<KeywordEntry> wallpaperKeywords = (List<KeywordEntry>)msg.obj;
			if (!wallpaperKeywords.isEmpty()) {
				for(KeywordEntry item:wallpaperKeywords){
					Utils.dout(TAG, item.getName());
					tagCloudView.addTag(new Tag(item.getName(), (int)(Math.random()*10+1), "http://www.google.com"));
				}
				tagCloudView.reset();	
			}
			
			break;

		default:
			break;
		}
		return false;
	}
	
}
